package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.entity.ReviewDetialEty;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class ReviewGameListViewAdapter extends BaseAdapter{
    private Context context;
    private List<ReviewDetialEty> datas;


    public ReviewGameListViewAdapter(Context context, List<ReviewDetialEty> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            int pos = position;
            MyViewHolderOne holderOne = null;
            MyViewHolderThree holderThree = null;
            int type = getItemViewType(position);
            if (convertView == null) {
                switch (type) {
                    case 0:
                        convertView = LayoutInflater.from(context)
                                .inflate(R.layout.item_listview_review_game_text, null);
                        holderOne = new MyViewHolderOne(convertView);
                        convertView.setTag(holderOne);
                        break;

                    case 1:
                        convertView = LayoutInflater.from(context)
                                .inflate(R.layout.item_listview_review_game_picture_three, null);
                        holderThree = new MyViewHolderThree(convertView);
                        convertView.setTag(holderThree);
                        break;
                    default:
                        break;
                }
            } else {
                switch (type) {
                    case 0:
                        holderOne = (MyViewHolderOne) convertView.getTag();
                        break;

                    case 1:
                        holderThree = (MyViewHolderThree) convertView.getTag();
                        break;
                    default:
                        break;
                }
            }
            switch (type) {
                case 0:
                    holderOne.picture.setDefaultImageResId(R.mipmap.ic_launcher);
                    holderOne.picture.setErrorImageResId(R.mipmap.ic_launcher);
                    holderOne.picture.setImageUrl(datas.get(pos).getImgsrc()
                            , VolleySingleton.getImageLoader());
                    holderOne.titleTv.setText(datas.get(pos).getTitle());
                    holderOne.introTv.setText(datas.get(pos).getDigest());
                    holderOne.countTv.setText("" + datas.get(pos).getPriority());
                    break;

                case 1:
                    holderThree.textView.setText(datas.get(pos).getTitle());
                    holderThree.pictureOne.setDefaultImageResId(R.mipmap.ic_launcher);
                    holderThree.pictureOne.setErrorImageResId(R.mipmap.ic_launcher);
                    holderThree.pictureOne.setImageUrl(datas.get(pos).getImgsrc()
                            , VolleySingleton.getImageLoader());

                    holderThree.pictureTwo.setDefaultImageResId(R.mipmap.ic_launcher);
                    holderThree.pictureTwo.setErrorImageResId(R.mipmap.ic_launcher);
                    holderThree.pictureTwo.setImageUrl(datas.get(pos).getImgextra().get(0).getImgsrc()
                            , VolleySingleton.getImageLoader());
                    holderThree.pictureThree.setDefaultImageResId(R.mipmap.ic_launcher);
                    holderThree.pictureThree.setErrorImageResId(R.mipmap.ic_launcher);
                    holderThree.pictureThree.setImageUrl(datas.get(pos).getImgextra().get(1).getImgsrc()
                            , VolleySingleton.getImageLoader());
                    break;
                default:
                    break;
            }

        return convertView;
    }


    @Override
    public int getItemViewType(int position) {
        if (null != datas.get(position).getImgextra()&&datas.get(position).getImgextra().size()==2){
            return 1;
        }else {
            return 0;
        }

    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    public void refreshData(List<ReviewDetialEty> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }



    class MyViewHolderOne{
        NetworkImageView picture;
        TextView titleTv;
        TextView introTv;
        TextView countTv;
        public MyViewHolderOne(View view){
            picture = (NetworkImageView) view.findViewById(R.id.iv_review_game_picture);
            titleTv = (TextView) view.findViewById(R.id.tv_review_game_text_title);
            introTv = (TextView) view.findViewById(R.id.tv_review_game_text_intro);
            countTv = (TextView) view.findViewById(R.id.tv_review_game_text_count);
        }
    }

    class MyViewHolderThree{
        TextView textView;
        NetworkImageView pictureOne;
        NetworkImageView pictureTwo;
        NetworkImageView pictureThree;
        public MyViewHolderThree(View itemView){
            textView = (TextView) itemView.findViewById(R.id.tv_review_game_text);
            pictureOne = (NetworkImageView) itemView.findViewById(R.id.iv_review_game_picture_one);
            pictureTwo = (NetworkImageView) itemView.findViewById(R.id.iv_review_game_picture_two);
            pictureThree = (NetworkImageView) itemView.findViewById(R.id.iv_review_game_picture_three);
        }
    }
}
